home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / BPDECODE.ZIP / BPDECODE.C next >
C/C++ Source or Header  |  1994-11-16  |  6KB  |  192 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. /* 
  35.  * DESCRIPTION: This file and program were modified on 11-10-1994 
  36.  *              to allow for the renaming of the uu output files 
  37.  *        for dos machines...  
  38.  * EXAMPLE: 
  39.  *    FILE.AAAA.bin was uuencoded into a file we'll call FILEAAAA.uu
  40.  *    on a UN*X machine and transported via sneakernet to an MS-DOS 
  41.  *    machine where I wish to uudecode it.  Because the filename is 
  42.  *    either too long or has two periods in it, it doesn't work.  
  43.  *    So this program and the command:
  44.  *        C:\> bgpuudec.exe FILEAAAA.uu outname.bin
  45.  *    will create FILE.AAAA.bin with the new name outname.bin
  46.  *
  47.  * LIMITATIONS: You can't use stdin and must do one file at a time.
  48.  *              As of yet, no error checking is done on new filename.
  49.  *
  50.  * BUGS: None that I know of except for some of the error messages
  51.  *       aren't very pretty.
  52.  *
  53.  * CONTRIBUTOR:
  54.  *      Brad Parks 
  55.  *      (bparks@intcorp.com)
  56.  *      2675 Patton Road 
  57.  *      St.Paul, MN  55113
  58.  */
  59.  
  60. /*
  61.  * uudecode [file] [outfile]
  62.  *
  63.  * create the specified outfile, decoding as you go.
  64.  * used with uuencode.
  65.  */
  66.  
  67. #include <sys/param.h>
  68. #include <sys/stat.h>
  69. #include <pwd.h>
  70. #include <stdio.h>
  71. #include <string.h>
  72.  
  73. char *filename, *outfile;
  74.  
  75. /* ARGSUSED */
  76. main(argc, argv)
  77.     int argc;
  78.     char **argv;
  79. {
  80.     extern int errno;
  81.     int rval;
  82.  
  83.     if (*++argv) {
  84.         rval = 0;
  85.  
  86.         if (!freopen(filename = *argv, "r", stdin)) {
  87.             (void)fprintf(stderr, "uudecode: %s: %s\n",
  88.                 *argv, "Whoops!");
  89.             rval = 1;
  90.         }
  91.  
  92.         if (*++argv) {
  93.             outfile = *argv;
  94.         } else {
  95.             outfile = 0;
  96.         } 
  97.         rval |= decode();
  98.     } else {
  99.         usage(*--argv);
  100.         rval = 1;
  101.     }
  102.     exit(rval);
  103. }
  104.  
  105. decode()
  106. {
  107.     extern int errno;
  108.     struct passwd *pw;
  109.     register int n;
  110.     register char ch, *p;
  111.     int mode, n1;
  112.     char buf[MAXPATHLEN];
  113.  
  114.     /* search for header line */
  115.     do {
  116.         if (!fgets(buf, sizeof(buf), stdin)) {
  117.             (void)fprintf(stderr,
  118.                 "uudecode: %s: no \"begin\" line\n", filename);
  119.             return(1);
  120.         }
  121.     } while (strncmp(buf, "begin ", 6));
  122.     (void)sscanf(buf, "begin %o %s", &mode, buf);
  123.  
  124.     /* create output file, set mode */
  125.     if (outfile != 0) {
  126.         strcpy(buf,outfile);
  127.     }
  128.     if (!freopen(buf, "w", stdout)) {
  129.         (void)fprintf(stderr, "uudecode: %s: %s: %s\n", buf,
  130.             filename, "strerror(errno)");
  131.         return(1);
  132.     }
  133.  
  134.     /* for each input line */
  135.     for (;;) {
  136.         if (!fgets(p = buf, sizeof(buf), stdin)) {
  137.             (void)fprintf(stderr, "uudecode: %s: short file.\n",
  138.                 filename);
  139.             return(1);
  140.         }
  141. #define    DEC(c)    (((c) - ' ') & 077)        /* single character decode */
  142.         /*
  143.          * `n' is used to avoid writing out all the characters
  144.          * at the end of the file.
  145.          */
  146.         if ((n = DEC(*p)) <= 0)
  147.             break;
  148.         for (++p; n > 0; p += 4, n -= 3)
  149.             if (n >= 3) {
  150.                 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
  151.                 putchar(ch);
  152.                 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  153.                 putchar(ch);
  154.                 ch = DEC(p[2]) << 6 | DEC(p[3]);
  155.                 putchar(ch);
  156.             }
  157.             else {
  158.                 if (n >= 1) {
  159.                     ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
  160.                     putchar(ch);
  161.                 }
  162.                 if (n >= 2) {
  163.                     ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  164.                     putchar(ch);
  165.                 }
  166.                 if (n >= 3) {
  167.                     ch = DEC(p[2]) << 6 | DEC(p[3]);
  168.                     putchar(ch);
  169.                 }
  170.             }
  171.     }
  172.     if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
  173.         (void)fprintf(stderr, "uudecode: %s: no \"end\" line.\n",
  174.             filename);
  175.         return(1);
  176.     }
  177.     return(0);
  178. }
  179.  
  180. usage(progname)
  181.     char *progname;
  182. {
  183.     (void)fprintf(stderr, "\tUSAGE: %s infile [outfile]\n",progname);
  184.     (void)fprintf(stderr, 
  185.         "\t  The uuencoded infile will be uudecoded\n");
  186.     (void)fprintf(stderr, 
  187.         "\t  and renamed to outfile (if specified).\n");
  188.     (void)fprintf(stderr, 
  189.         "\t  stdin is not a valid input source.\n");
  190.     exit(1);
  191. }
  192.